package com.zp.qualitypractice.list;

import android.animation.Animator;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

import java.util.List;

/**
 * Created by izp on 2017/4/22.
 */

public abstract class CustomLoadingAnimationController {
    private View mTarget;
    private List<Animator> mAnimators;

    public View getmTarget() {
        return mTarget;
    }

    public CustomLoadingAnimationController setmTarget(View mTarget) {
        this.mTarget = mTarget;
        return this;
    }

    public int getWidth() {
        return mTarget.getWidth();
    }

    public int getHeight() {
        return mTarget.getHeight();
    }

    /**
     * 让targetView 在子线程重绘
     */
    public void postInvalidate() {
        mTarget.postInvalidate();
    }

    public void initAnimation(){
        mAnimators = createAnimation();
    }

    public void setAnimationStatus(AnimStatus animStatus){
        if (null==mAnimators){
            return;
        }
        for (Animator animator:mAnimators){
            boolean isAnimationRuning = animator.isRunning();

            switch (animStatus){
                case START:
                    if (!isAnimationRuning){
                        animator.start();
                    }
                    break;
                case EDN:
                    if (isAnimationRuning){
                        animator.end();
                    }
                    break;
                case CANCEL:
                    if (isAnimationRuning){
                        animator.cancel();
                    }
                    break;
            }
        }
    }


    public abstract void draw(Canvas canvas, Paint paint);
    public abstract List<Animator> createAnimation();

    protected enum AnimStatus{
        START,EDN,CANCEL
    }

}
